<template>
  <a-layout
    :right-style="{ overflow: 'hidden' }"
    :style="{ minHeight: 'calc(100vh - 88px)' }"
  >
    <a-layout-content>
      <!-- 表格 -->
      <VxeBasicTable ref="TableRefData" v-bind="options">
        <template #toolbar_buttons>
          <ToolBtn
            :create-call="openEdit"
            :delete-call="removeBatch"
            :show-export="false"
            :show-import="false"
            :show-delete="false"
          />
        </template>
        <template #action="{ row }">
          <Action :record="row" @edit="openEdit" @delete="remove" />
        </template>
      </VxeBasicTable>
      <!-- 编辑弹窗 -->
      <dict-data-edit
        v-model:visible="showEdit"
        :data="current"
        :dict-id="dictId"
        @done="reload"
      />
    </a-layout-content>
  </a-layout>
</template>
<script lang="ts" setup>
  import { createVNode, ref, watch } from 'vue';
  import { message, Modal } from 'ant-design-vue/es';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { messageLoading } from 'ele-admin-pro/es';
  import { useConfig } from './config';
  import DictDataEdit from './dict-data-edit.vue';
  import {
    pageDictionaryData,
    removeDictionaryData,
    removeDictionaryDataBatch
  } from '@/api/system/dictionary-data';
  import type { DictionaryData } from '@/api/system/dictionary-data/model';

  const props = defineProps<{
    // 字典id
    dictId: number;
  }>();

  // 表格实例
  const { TableRefData, ToolBtn, Action, options, defaultWhere } =
    useConfig(pageDictionaryData);

  // 表格选中数据
  const selection = ref<DictionaryData[]>([]);

  // 当前编辑数据
  const current = ref<DictionaryData | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRefData.value?.commitProxy('query');
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: DictionaryData) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 删除单个 */
  const remove = (row: DictionaryData) => {
    const hide = messageLoading('请求中..', 0);
    removeDictionaryData(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的字典项吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = messageLoading('请求中..', 0);
        removeDictionaryDataBatch(selection.value.map((d) => d.id))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  // 监听字典id变化
  watch(
    () => props.dictId,
    () => {
      defaultWhere.dictTypeId = props.dictId;
      reload();
    }
  );
</script>

<style lang="less" scoped>
  .sys-dict-data-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-dict-data-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
</style>
